﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MaterialEditor
{
    public partial class Timeline : Form
    {
        private bool isPlaying;

        private long then;

        public Timeline()
        {
            InitializeComponent();

            // setup the current time label
            playbackPannel1_CursorPointMoved();

            // ensure this window remains on top of the main window
            TopMost = true;
        }

        private void playbackPannel1_CursorPointMoved()
        {
            string secs = playbackPannel1.currentTime.Seconds.ToString();
            string mils = playbackPannel1.currentTime.Milliseconds.ToString();

            label1.Text = (secs.Length < 2 ? '0' + secs : secs) + ':' + (mils.Length < 2 ? '0' + mils : mils);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            isPlaying = true;
            playbackPannel1.Focus();
        }

        public void DoUpdate()
        {
            // advance the playback if the form is playing animation
            if (isPlaying)
            {
                long elapsed = DateTime.Now.Ticks - then;
                playbackPannel1.currentTime += TimeSpan.FromTicks(elapsed);
            }

            then = DateTime.Now.Ticks;
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            isPlaying = false;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            playbackPannel1.MaxTime = TimeSpan.FromSeconds((double)numericUpDown1.Value);
        }
    }
}
